package com.coldchain.cys.api;


import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.cys.entity.CysChild;
import com.coldchain.cys.entity.SysMessage;
import com.coldchain.cys.mapping.ApiUtils;
import com.coldchain.cys.mapping.MappingUtils;
import com.coldchain.cys.page.SysMessageQueryVo;
import com.coldchain.cys.service.ICysService;
import com.coldchain.cys.service.ISysMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tianpeng
 * @since 2018-08-28
 */
@Controller
@CheckUserToken
@Api(description="承运商系统消息模块")
public class SysMessageController {

	@Autowired
	private ISysMessageService sysMessageService ;

	@Autowired
	private ICysService cysService ;
	
	@ResponseBody
	@ApiOperation(value="推送系统消息")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="cid",value="承运商id",dataType="Long",paramType="query"),
		@ApiImplicitParam(name="title",value="标题",dataType="String",paramType="query"),
		@ApiImplicitParam(name="content",value="内容",dataType="String",paramType="query")
	})
	@RequestMapping(value=ApiUtils.DO_PUSH_CYS_MESSAGE,method={RequestMethod.POST})
	public BaseResponse doPushCysMessage(@ApiIgnore SysMessage sysMessage){
		return sysMessageService.pushCysMessage(sysMessage) ;
	}
	
	@ResponseBody
	@ApiOperation(value="查询承运商消息列表")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="cid",value="承运商id",dataType="Long",paramType="query"),
		@ApiImplicitParam(name="current",value="页码",dataType="Integer",paramType="query"),
	})
	@RequestMapping(value=ApiUtils.SHOW_CYS_MESSAGE_LIST,method={RequestMethod.GET})
	public ObjectRestResponse<SysMessageQueryVo> showCysMessageList(SysMessageQueryVo vo){
		sysMessageService.getCysMessageList(vo) ;
		return new ObjectRestResponse<SysMessageQueryVo>(vo) ;
	}

	@ResponseBody
	@ApiIgnore
	@RequestMapping(value=ApiUtils.SHOW_CYS_MESSAGE_DETAIL + "/{id}",method={RequestMethod.GET})
	public ObjectRestResponse<SysMessage> showCysMessageDetail(@PathVariable Integer id){
		SysMessage sysMessage = sysMessageService.getCysMessageDetail(id);
		return new ObjectRestResponse<SysMessage>(sysMessage) ;
	}
	
	/**
	 * 消息详情页面
	 * @param id 消息的id
	 * @return
	 */
	@ApiIgnore
	@RequestMapping(value=MappingUtils.SHOW_CYS_MESSAGE_DETAIL_PAGE)
	public ModelAndView showCysMessageDetailPage(Long id) {
		SysMessage sysMessage = sysMessageService.getById(id);
		ModelAndView m = new ModelAndView(MappingUtils.SHOW_CYS_MESSAGE_DETAIL_PAGE);
		CysChild current = cysService.getCurrentLogin();
		m.addObject("sysMessage", sysMessage) ;
		m.addObject("current", current) ;
		return m ;
	}

}
